package yz.cp.back.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import yz.cp.back.auth.security.SecurityUtils;
import yz.cp.back.common.constant.CommonConstant;
import yz.cp.back.common.utils.PageKit;
import yz.cp.back.common.vo.SearchVo;
import yz.cp.back.s.entity.SysRole;
import yz.cp.back.s.entity.SysUser;
import yz.cp.back.s.service.ISysRoleService;
import yz.cp.back.s.service.ISysUserRoleService;
import yz.cp.back.s.service.ISysUserService;

import java.util.List;

@Service
public class UserManage {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysUserRoleService  userRoleService;
    public IPage<SysUser> findByCondition(SysUser user, SearchVo searchVo, Pageable pageable) {

        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        IPage<SysUser> page= PageKit.buildPage(pageable,wrapper);
        //模糊搜素
        if(StringUtils.isNotBlank(user.getUsername())){
            wrapper.like("username", user.getUsername());
        }
        if(StringUtils.isNotBlank(user.getMobile())){
            wrapper.like("mobile",user.getMobile());
        }
        if(StringUtils.isNotBlank(user.getEmail())){
            wrapper.like("email",user.getEmail());
        }

        //部门
        if(StringUtils.isNotBlank(user.getDepartmentId())){
            wrapper.eq("department_id",user.getDepartmentId());
        }

        //性别
        if(user.getSex()!=null){
            wrapper.eq("sex",user.getSex());
        }
        //类型
        if(user.getType()!=null){
            wrapper.eq("type",user.getType());
        }
        //状态
        if(user.getStatus()!=null){
            wrapper.eq("status",user.getStatus());
        }
        //创建时间
        if(searchVo.getStartDate()!=null&&searchVo.getEndDate()!=null){
            wrapper.between("create_time",searchVo.getStartDate(),searchVo.getEndDate());
        }

        //数据权限
        List<String> depIds = getDepartmentIds();
        if(depIds!=null&&depIds.size()>0){
            wrapper.in("","");
        }
       return userService.page(page,wrapper);
    }
    public List<String> getDepartmentIds(){
        SysUser u =userService.findByUsername( SecurityUtils.getSecUser().getUsername());
        // 用户角色
        List<SysRole> userRoleList = userRoleService.findByUserId(u.getId());
        // 判断有无全部数据的角色
        Boolean flagAll = false;
        for(SysRole r : userRoleList){
            if(r.getDataType()==null||r.getDataType().equals(CommonConstant.DATA_TYPE_ALL)){
                flagAll = true;
                break;
            }
        }
        if(flagAll){
            return null;
        }
        // 查找自定义
        return  userRoleService.findDepIdsByUserId(u.getId());
    }
}
